package com.heu.blood.transfusion.controller;

import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.GenerateNumberBySnowFlake;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.transfusion.entity.TransfusionAgreementEntity;
import com.heu.blood.transfusion.service.TransfusionAgreementService;
import com.heu.blood.transfusion.vo.TransfusionAgreementVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;




/**
 * 
 *
 * @author myq
 * @email 596921409@qq.com
 * @date 2023-08-03 18:45:33
 */
@RestController
@Slf4j
@RequestMapping("transfusion/transfusionagreement")
public class TransfusionAgreementController {
    @Autowired
    private TransfusionAgreementService transfusionAgreementService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "输血同意书", operate = "分页查询输血同意书信息")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = transfusionAgreementService.queryPage(params);


        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ControllerLogAnnotation(module = "输血同意书", operate = "根据id查询输血同意书信息")
    public R info(@PathVariable("id") Long id){
		TransfusionAgreementEntity transfusionAgreement = transfusionAgreementService.getById(id);

        return R.ok().put("transfusionAgreement", transfusionAgreement);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "输血同意书", operate = "保存输血同意书信息")
    public R save(@RequestBody TransfusionAgreementEntity transfusionAgreement){
        String flag ="TA";
        GenerateNumberBySnowFlake transfusionId = new GenerateNumberBySnowFlake("transfusionId", flag);
        log.info(transfusionId.genNumberByflag());
        transfusionAgreement.setTransfusionId(transfusionId.genNumberByflag());
        transfusionAgreementService.save(transfusionAgreement);

        return R.ok("输血同意书通过");
//        String flag ="TA";
//        GenerateNumberBySnowFlake transfusionId = new GenerateNumberBySnowFlake("transfusionId", flag);
//        log.info(transfusionId.genNumberByflag());
//        transfusionAgreement.setTransfusionId(transfusionId.genNumberByflag());
//        String signPatient = transfusionAgreement.getSignPatient();
//        String signDoctor = transfusionAgreement.getSignDoctor();
//        String signFamily = transfusionAgreement.getSignFamily();
//        if(signDoctor!=null&&(signFamily!=null||signPatient!=null)){
//            transfusionAgreement.setFinalSuggestion("同意");
//            transfusionAgreementService.save(transfusionAgreement);
//            return R.ok("输血同意书通过");
//        }
//        transfusionAgreement.setFinalSuggestion("不同意");
//        return R.error("输血同意书不通过");

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ControllerLogAnnotation(module = "输血同意书", operate = "修改输血同意书信息")
    public R update(@RequestBody TransfusionAgreementEntity transfusionAgreement){
		transfusionAgreementService.updateById(transfusionAgreement);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "输血同意书", operate = "批量删除输血同意书信息")
    public R delete(@RequestBody Long[] ids){
		transfusionAgreementService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取输血申请同意书的打印预览内容
     * @param transfusionId
     * @return
     */
    @GetMapping("/getAgreement")
    @ControllerLogAnnotation(module = "输血同意书", operate = "获取输血申请同意书的打印预览内容")
    public R getAgreement(String transfusionId){
        TransfusionAgreementVo transfusionAgreementVo = transfusionAgreementService.getAgreement(transfusionId);
        return R.ok().put("data", transfusionAgreementVo);
    }

}
